package com.leyou.page.service;

import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.seckill.client.SeckillClient;
import com.leyou.seckill.dto.SeckillPolicyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageService {

    @Autowired
    private ItemClient itemClient;

    public Map buildDataBySpuId(Long spuId) {
        Map mapData = new HashMap();
        SpuDTO spu = itemClient.findSpuBySpuId(spuId);
//        spuName       spu对象的名成
        mapData.put("spuName",spu.getName());
//        subTitle      spu对象的子标题
        mapData.put("subTitle",spu.getSubTitle());
//        categories    spu三级分类集合
        List<Long> categoryIds = spu.getCategoryIds(); //三级分类的ids
        List<CategoryDTO> categoryList = itemClient.findCategoryListByIds(categoryIds);
        mapData.put("categories",categoryList);
//        brand         品牌的对象
        Long brandId = spu.getBrandId();
        BrandDTO brand = itemClient.findBrandById(brandId);
        mapData.put("brand",brand);
//        detail        SPUDetail对象
        SpuDetailDTO detail = itemClient.findSpuDetailBySpuId(spuId);
        mapData.put("detail",detail);
//        skus          当前spu下sku集合
        List<SkuDTO> skuDTOS = itemClient.findSkuListBySpuId(spuId);
        mapData.put("skus",skuDTOS);
//        specs         规格组的集合，每个规格组下都所属的规格参数集合
        List<SpecGroupDTO> specGroupDTOS = itemClient.findSpecGroupWithParamsByCategoryId(spu.getCid3());
        mapData.put("specs",specGroupDTOS);

        return mapData;
    }

    public void removeHtml(Long spuId) {
        File file = new  File("D:\\class118-leyou\\nginx-1.16.0\\html\\item\\"+spuId+".html");
        file.delete();
        System.out.println("静态页面删除成功");
    }

    @Autowired
    private TemplateEngine templateEngine;
    public void createHtml(Long spuId) {
        Context context = new Context();
        context.setVariables(this.buildDataBySpuId(spuId));
        try(PrintWriter writer = new PrintWriter("D:\\class118-leyou\\nginx-1.16.0\\html\\item\\"+spuId+".html")) {  //流的关闭是自动的
            templateEngine.process("item",context,writer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("静态页面创建成功");
    }


    @Autowired
    private SeckillClient seckillClient;

    public void createSeckillPage(String nowDate) {
//        生成两套页面 1、列表 2、详情
        List<SeckillPolicyDTO> seckillPolicyList = seckillClient.findSeckillListByDate(nowDate);
//        1、列表
        createSecKillIndexPage(seckillPolicyList);

        for (SeckillPolicyDTO seckillPolicyDTO : seckillPolicyList) {
            createSecKillDetailPage(seckillPolicyDTO);
        }

    }

    //创建秒杀的列表页面
    private void createSecKillIndexPage(List<SeckillPolicyDTO> seckillPolicyList){
        Context context = new Context();
//        seckillList: 当天秒杀的商品数据
//       通过feign远程调用 seckill微服务提供的方法
        context.setVariable("seckillList" ,seckillPolicyList);
        try(PrintWriter writer = new PrintWriter("D:\\class118-leyou\\nginx-1.16.0\\html\\seckill\\list.html")) {  //流的关闭是自动的
            templateEngine.process("seckill-index",context,writer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    //创建秒杀的详情页面
    private void createSecKillDetailPage(SeckillPolicyDTO seckillPolicyDTO) {
//        detail  SPUDetail对象
//        seckillgoods 当前秒杀商品对象
//        specs  规格组对象 每个规格组带有规格参数的集合
        Long seckillId = seckillPolicyDTO.getId();
        Long spuId = seckillPolicyDTO.getSpuId();
        Context context = new Context();
//        seckillList: 当天秒杀的商品数据
//       通过feign远程调用 seckill微服务提供的方法

        SpuDetailDTO spuDetail = itemClient.findSpuDetailBySpuId(spuId);
        context.setVariable("detail" ,spuDetail);

        context.setVariable("seckillgoods" ,seckillPolicyDTO);

        List<SpecGroupDTO> specGroupList = itemClient.findSpecGroupWithParamsByCategoryId(seckillPolicyDTO.getCid3());
        context.setVariable("specs" ,specGroupList);
        try(PrintWriter writer = new PrintWriter("D:\\class118-leyou\\nginx-1.16.0\\html\\seckill\\"+seckillId+".html")) {  //流的关闭是自动的
            templateEngine.process("seckill-item",context,writer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


}
